//
//  SystemViewController.m
//  IhuayuanClient
//
//  Created by 黄思清 on 2017/7/6.
//  Copyright © 2017年 com.ihuayuan. All rights reserved.
//

#import "SystemViewController.h"
#import "LoginViewController.h"
#import "SystemTableViewCell.h"
#import "AboutMeWebViewController.h"
#import "ChangePhoneViewController.h"
#import "ChangPassWordViewController.h"

@interface SystemViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableView;

@end

@implementation SystemViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavgation];
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self createView];
    
}

#pragma mark - initView
- (void)createView{
    
    _tableView = [[UITableView alloc] init];
    _tableView.frame = CGRectMake(0, 64, MAIN_SCREEN_WIDTH, MAIN_SCREEN_HEIGHT - 64);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 50;
    _tableView.sectionHeaderHeight = 0;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[SystemTableViewCell class] forCellReuseIdentifier:@"mysystemcell"];
    
    //退出按钮
    UIView *footerview = [[UIView alloc] initWithFrame:CGRectMake(0,0, MAIN_SCREEN_WIDTH ,104)];
    footerview.backgroundColor = [UIColor clearColor];
    UIButton *loginoutBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, 50, MAIN_SCREEN_WIDTH-20, 44)];
    loginoutBtn.layer.cornerRadius = 5.0;
    [loginoutBtn setTitle:@"安全退出" forState:UIControlStateNormal];
    [loginoutBtn setBackgroundColor:UIColors(MainColor)];
    [loginoutBtn setTintColor:[UIColor whiteColor]];
    [loginoutBtn addTarget:self action:@selector(LoginOut) forControlEvents:UIControlEventTouchUpInside];
    [footerview addSubview:loginoutBtn];
    _tableView.tableFooterView = footerview;
    [self.view addSubview:self.tableView];
    
    
}
-(void)LoginOut
{
    UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"消息提示框" message:@"是否确认退出" preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *act = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:nil];
    UIAlertAction *commit = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action)
    {
        [UserInfoManage logout];
        UIWindow *window = [[UIApplication sharedApplication].windows firstObject];
        LoginViewController *VC = [[LoginViewController alloc]init];
        window.rootViewController = VC;
    }];
    [ac addAction:act];
    [ac addAction:commit];
    [self presentViewController:ac animated:YES completion:nil];
}



#pragma mark - tableView代理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SystemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mysystemcell" forIndexPath:indexPath];
    cell.lineV = false;
    if(indexPath.row==0){
        cell.titL.text = @"关于我们";
        [cell.TitimageV setImage:[UIImage imageNamed:@"aboutme"]];
    }else if (indexPath.row==1){
        cell.titL.text = @"客服电话";
        [cell.TitimageV setImage:[UIImage imageNamed:@"KeFu"]];
    }else if (indexPath.row==2){
        cell.titL.text = @"清除缓存";
        float intg = [[SDImageCache sharedImageCache] getSize]/ 1024 /1024;
        cell.rightL.text = [NSString stringWithFormat:@"%.2fMB",intg];
        [cell.TitimageV setImage:[UIImage imageNamed:@"huancun"]];
    }else if (indexPath.row==3){
        cell.titL.text = @"系统版本";
        cell.rightL.text =  [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
        [cell.TitimageV setImage:[UIImage imageNamed:@"xitong"]];
        cell.lineV = YES;
    }else if (indexPath.row == 4)
    {
        cell.titL.text = @"变更手机号";
        [cell.TitimageV setImage:[UIImage imageNamed:@"qiehuan"]];
    }else if (indexPath.row == 5)
    {
        cell.titL.text = @"修改密码";
        [cell.TitimageV setImage:[UIImage imageNamed:@"ChangPWD"]];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [_tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if(indexPath.row==0)
    {
        AboutMeWebViewController *VC = [[AboutMeWebViewController alloc]init];
        VC.url = @"/index.php?g=App&m=Index&a=aboutus";
        [self.navigationController pushViewController:VC animated:YES];
    }else if (indexPath.row == 1)
    {
        UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"是否拨打400-928-0087" message:@"" preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction *act = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
        UIAlertAction *commit = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:0519-85788690"];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        }];

        [ac addAction:act];
        [ac addAction:commit];
        [self presentViewController:ac animated:YES completion:nil];

    }else if (indexPath.row == 2)
    {
         [self clearCache];
        [self.tableView reloadData];
    }else if (indexPath.row ==3 )
    {
        
    }else if (indexPath.row == 4)
    {
        ChangePhoneViewController *VC = [[ChangePhoneViewController alloc]init];
        [self.navigationController pushViewController:VC animated:YES];
    }else if (indexPath.row == 5)
    {
//        NSLog(@"修改密码");
        ChangPassWordViewController *VC = [[ChangPassWordViewController alloc]init];
        [self.navigationController pushViewController:VC animated:YES];
    }
}
-(void)clearCache
{
    UIWindow *window = [[UIApplication sharedApplication].windows firstObject];
    [[SDImageCache sharedImageCache] clearDisk];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text =@"清除缓存成功";
    [hud hideAnimated:YES afterDelay:1];
//    float intg = [[SDImageCache sharedImageCache] getSize]/ 1024 /1024;
////    self.clear.detaileL.text = [NSString stringWithFormat:@"%.2fMB",intg];
}


//设置导航
-(void)setNavgation{
    UIColor *bgColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"nav_bg"]];
    UIView *navigationBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MAIN_SCREEN_WIDTH, 64)];
    [navigationBar setBackgroundColor:bgColor];
    //左侧按钮
    UIButton *leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 20, 44, 44)];
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"left"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(leftOnClick) forControlEvents:UIControlEventTouchUpInside];
    [navigationBar addSubview:leftBtn];
    
    //中间信息
    UILabel *titLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, MAIN_SCREEN_WIDTH, 44)];
    [titLabel setTextColor:[UIColor whiteColor]];
    [titLabel setTextAlignment:1];
    [titLabel setText:@"系统设置"];
    [navigationBar addSubview:titLabel];
    
    [self.view addSubview:navigationBar];
}

-(void)leftOnClick{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
